﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Bll.CommonBll;
using CSP.Bll.IObjectBlls;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Common.Factories;
using CSP.Dao;
using CSP.Model.CSP;
using System.Collections.ObjectModel;

namespace CSP.Bll.ObjectBlls
{
    public class ServiceTypeBll : CSPCommonBll, IServiceTypeBll
    {
        private const string CACHEKEYALL = "ServiceTypeBllAll";

        private IServiceTypeDao _serviceTypeDao;

        public ServiceTypeBll()
        {
            _cacheProvider = CachedProviderFactory.GetLocalCachedProvider();
            _serviceTypeDao = CSPDaoFactory.GetInstance().GetServiceTypeDao();
        }

        public ICollection<ServiceType> FindAll()
        {
            //检测缓存是否存在
            ICollection<ServiceType> serviceTypes = _cacheProvider.GetData<ServiceType[]>(CACHEKEYALL);
            if (serviceTypes == null)
            {
                //从数据库读取数据
                serviceTypes = _serviceTypeDao.FindAll();
                _cacheProvider.AddData(CACHEKEYALL, serviceTypes, DateTime.Now.AddHours(2));
            }
            return serviceTypes;
        }

        public ICollection<ServiceType> FindAllByAvailable()
        {
            ICollection<ServiceType> serviceTypes = this.FindAll();
            ICollection<ServiceType> result = null;
            if (serviceTypes != null)
            {
                var temServiceTypes = serviceTypes.Where(s => s.Status == false);
                if (temServiceTypes != null)
                {
                    result = temServiceTypes.ToArray();
                }
            }
            return result;
        }

        /// <summary>
        /// 根据Id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ServiceType GetDataById(int id)
        {
            ServiceType derviceType = GetById(id);
            if (derviceType == null)
            {
                FindAll();
                derviceType = GetById(id);
            }

            return derviceType;
        }

        public ICollection<ServiceType> GeDatatByIds(int[] Ids)
        {
            throw new NotImplementedException();
        }

        public void Add(ServiceType obj)
        {
            throw new NotImplementedException();
        }

        public void Update(ServiceType obj)
        {
            throw new NotImplementedException();
        }

        public void DeleteById(int id)
        {
            throw new NotImplementedException();
        }

        public void RefreshCache()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 通过Id 获取领域类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private ServiceType GetById(int id)
        {
            ServiceType serviceType = null;

            //检测缓存是否存在
            ICollection<ServiceType> serviceTypes = GetByIds(new int[] { id });
            if (serviceTypes != null && serviceTypes.Count != 0)
            {
                serviceType = serviceTypes.FirstOrDefault();
            }
            return serviceType;
        }

        private ICollection<ServiceType> GetByIds(int[] ids)
        {
            ICollection<ServiceType> result = new Collection<ServiceType>();

            //检测缓存是否存在
            ICollection<ServiceType> serviceTypes = _cacheProvider.GetData<ServiceType[]>(CACHEKEYALL);
            if (serviceTypes == null || serviceTypes.Count == 0)
            {
                serviceTypes = FindAll();
                if (serviceTypes == null || serviceTypes.Count == 0)
                {
                    return result;
                }
            }

            foreach (var item in serviceTypes)
            {
                if (ids.Contains(item.Id))
                {
                    result.Add(item);
                }
            }
            return result;
        }
    }
}
